using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Text.RegularExpressions;

public partial class Pages_Employee_ManageFaultDeliveries_AddFaultDelivery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            cldFaultDate.SelectedDate = DateTime.Today;
    }

    // Checks whether the Refund field is a positive decimal number
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal n = decimal.Parse(args.Value);
            args.IsValid = (n >= 0);
        }
        catch (FormatException ex)
        {
            args.IsValid = false;
        }
    }

    // Checks whether the selected date is valid, it must be in the past or today
    protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (DateTime.Compare(cldFaultDate.SelectedDate, DateTime.Today) > 0)
        {
            args.IsValid = false;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;

        int deliverableID = Int32.Parse(txtDeliverableID.Text.Trim());
        string faultDate = cldFaultDate.SelectedDate.ToString("MM/dd/yyyy");
        string reason = txtReason.Text.Trim();
        decimal refundAmount = decimal.Parse(txtRefundAmount.Text);
        bool isRepaid = cboIsRepaid.Checked;

        int affected;
        try
        {
            affected = DALFaultDelivery.InsertFaultDelivery(deliverableID, faultDate, reason, refundAmount, isRepaid);
            if (affected > 0)
                lblMessage.Text = "Added successfully";
            else
                lblMessage.Text = "Error adding fault delivery. Please note that deliverable ID must exists and cannot be duplicated in Fault Deliveries.";
        }
        catch
        {
            lblMessage.Text = "Error adding fault delivery. Please note that deliverable ID must exists and cannot be duplicated in Fault Deliveries.";
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtDeliverableID.Text = "";
        cldFaultDate.SelectedDate = DateTime.Today;
        txtReason.Text = "";
        txtRefundAmount.Text = "";
        cboIsRepaid.Checked = false;
        txtDeliverableID.Focus();
    }

    /// <summary>
    /// This webmethod checks whether an deliverable ID is existed
    /// </summary>
    /// <param name="id">id</param>
    /// <returns>1 if the parameter is an id of an existed deliverable, 0 otherwise, -1 if id string is not a valid id</returns>
    [WebMethod]
    public static int IsExistedID(string id)
    {
        Regex idPattern = new Regex("^[1-9][0-9]*$");
        if (!idPattern.IsMatch(id))
            return -1;
        int n = Int32.Parse(id);
        if (DALDeliverable.GetDeliverablesByDeliverableID(n).Count != 0)
            return 1;
        else
            return 0;
    }
}
